<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:b="http://schemas.microsoft.com/office/word/2004/10/bibliography" version="1.0">
    <xsl:template match="zs:searchRetrieveResponse" xmlns:zs="http://www.loc.gov/zing/srw/">
        <!--<SRU-Response>
            <xsl:attribute name="numberOfSources">
                <xsl:value-of select="zs:numberOfRecords"/>
            </xsl:attribute>
            <xsl:attribute name="modVersion">
                <xsl:value-of select="zs:version"/>
            </xsl:attribute>
            <xsl:apply-templates select="zs:diagnostics/diag:diagnostic"
                xmlns:diag="http://www.loc.gov/zing/srw/diagnostic/"/>-->
            <xsl:apply-templates select="zs:records"/>
        <!--</SRU-Response>-->
    </xsl:template>

    <xsl:template match="zs:records" xmlns:zs="http://www.loc.gov/zing/srw/">
        <b:Sources>
            <xsl:apply-templates select="zs:record"/>
        </b:Sources>
    </xsl:template>

    <xsl:template match="zs:record" xmlns:zs="http://www.loc.gov/zing/srw/">
        <b:Source>
            <b:Tag/>
            <b:SourceType>Book</b:SourceType>
            <b:Title>
                <xsl:apply-templates select="zs:recordData/ns1:mods/ns1:titleInfo"
                    xmlns:ns1="http://www.loc.gov/mods/v3"/>
            </b:Title>
            <b:Year>
                <xsl:value-of select="zs:recordData/ns1:mods/ns1:originInfo/ns1:dateIssued"
                    xmlns:ns1="http://www.loc.gov/mods/v3"/>
            </b:Year>
            <b:StandardNumber>
                <xsl:apply-templates
                    select="zs:recordData/ns1:mods/ns1:identifier[attribute::type='isbn']"
                    xmlns:ns1="http://www.loc.gov/mods/v3">
                    <xsl:sort order="descending"/>
                </xsl:apply-templates>
            </b:StandardNumber>
            <b:City>
                <xsl:apply-templates select="zs:recordData/ns1:mods/ns1:originInfo/ns1:place"
                    xmlns:ns1="http://www.loc.gov/mods/v3"/>
            </b:City>
            <b:Publisher>
                <xsl:value-of select="zs:recordData/ns1:mods/ns1:originInfo/ns1:publisher"
                    xmlns:ns1="http://www.loc.gov/mods/v3"/>
            </b:Publisher>
            <b:Author>
                <b:Author>
                    <b:NameList>
                        <xsl:apply-templates
                            select="zs:recordData/ns1:mods/ns1:name[attribute::type='personal']"
                            xmlns:ns1="http://www.loc.gov/mods/v3">
                            <xsl:sort select="@usage='primary'" order="descending"/>
                            <xsl:with-param name="type">personal</xsl:with-param>
                        </xsl:apply-templates>
                    </b:NameList>
                </b:Author>
            </b:Author>
            <b:StateProvince/>
            <b:CountryRegion/>
            <b:Volume>
                <xsl:apply-templates select="zs:recordData/ns1:mods/ns1:titleInfo/ns1:partNumber"
                    xmlns:ns1="http://www.loc.gov/mods/v3"/>
            </b:Volume>
            <b:NumberVolumes/>
            <b:ShortTitle>
                <xsl:apply-templates select="zs:recordData/ns1:mods/ns1:titleInfo"
                    xmlns:ns1="http://www.loc.gov/mods/v3">
                    <xsl:with-param name="type">alternative</xsl:with-param>
                </xsl:apply-templates>
            </b:ShortTitle>
            <b:Pages/>
            <b:Edition>
                <xsl:apply-templates select="zs:recordData/ns1:mods/ns1:originInfo/ns1:edition"
                    xmlns:ns1="http://www.loc.gov/mods/v3"/>
            </b:Edition>
            <b:Comments/>
            <b:RefOrder/>
        </b:Source>
    </xsl:template>

    <!-- ISBN -->
    <xsl:template match="ns1:identifier" xmlns:ns1="http://www.loc.gov/mods/v3">
        <xsl:if test="position() = 1">ISBN <xsl:value-of select="."/>
        </xsl:if>
    </xsl:template>

    <!-- Titel -->
    <xsl:template match="ns1:titleInfo" xmlns:ns1="http://www.loc.gov/mods/v3">
        <xsl:param name="type"/>
        <xsl:choose>
            <xsl:when test="@type">
                <xsl:if test="@type=$type">
                    <xsl:value-of select="ns1:title"/>
                    <xsl:if test="ns1:title">. <xsl:value-of select="ns1:subTitle"/>
                    </xsl:if>
                </xsl:if>
            </xsl:when>
            <xsl:otherwise>
                <xsl:if test="$type = ''">
                    <xsl:value-of select="ns1:title"/>
                    <xsl:if test="ns1:title">. <xsl:value-of select="ns1:subTitle"/>
                    </xsl:if>
                </xsl:if>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- Ort -->
    <xsl:template match="ns1:place" xmlns:ns1="http://www.loc.gov/mods/v3">
        <xsl:apply-templates select="ns1:placeTerm"/>
    </xsl:template>
    <xsl:template match="ns1:placeTerm" xmlns:ns1="http://www.loc.gov/mods/v3">
        <xsl:if test="@type='text' and position()=1">
            <xsl:value-of select="."/>
        </xsl:if>
    </xsl:template>

    <!-- Personen -->
    <xsl:template match="ns1:name" xmlns:ns1="http://www.loc.gov/mods/v3">
        <b:Person>
            <xsl:apply-templates select="ns1:namePart"/>
        </b:Person>
    </xsl:template>
    <xsl:template match="ns1:namePart" xmlns:ns1="http://www.loc.gov/mods/v3">
        <xsl:choose>
            <xsl:when test="@type='family'">
                <b:Last>
                    <xsl:value-of select="."/>
                </b:Last>
            </xsl:when>
            <xsl:when test="@type='given'">
                <b:First>
                    <xsl:value-of select="."/>
                </b:First>
            </xsl:when>
            <xsl:otherwise><!-- ignore --></xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- Band -->
    <xsl:template match="ns1:partNumber" xmlns:ns1="http://www.loc.gov/mods/v3">
        <xsl:choose>
            <xsl:when test="contains(.,'Bd. ')">
                <xsl:value-of select="substring-after(.,'Bd. ')"/>
            </xsl:when>
            <xsl:when test="contains(.,'Bd.')">
                <xsl:value-of select="substring-after(.,'Bd.')"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- Auflage -->
    <!-- Es wird versucht unnötige Informationen zu entfernen. Gelingt das wird "Aufl." oder "ed." je nach Sprache der Quelle angehängt. -->
    <xsl:template match="ns1:edition" xmlns:ns1="http://www.loc.gov/mods/v3">
        <xsl:choose>
            <xsl:when test="number(substring(.,1,1)) and contains(., ',')">
                <xsl:value-of select="substring-before(., ',')"/>
                <xsl:choose>
                    <xsl:when test="../../ns1:language/ns1:languageTerm = 'ger'"> Aufl.</xsl:when>
                    <xsl:when test="../../ns1:language/ns1:languageTerm = 'eng'"> ed.</xsl:when>
                    <xsl:otherwise> Aufl.</xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- Error -->
    <xsl:template match="diag:diagnostic" xmlns:diag="http://www.loc.gov/zing/srw/diagnostic/">
        <Exception>
            <Message>
                <xsl:value-of select="diag:message"/>
            </Message>
            <Details>
                <xsl:value-of select="diag:details"/>
            </Details>
        </Exception>
    </xsl:template>
</xsl:stylesheet>
